import React, {Component, useContext, useState} from 'react';
import {
    TextInput,
    Dimensions,
    Keyboard, View
} from 'react-native';
import Toast from "react-native-simple-toast";
import LocaleContext from "../libs/locale";
const {width,height,scale} = Dimensions.get("window");



type Props = {};
type State = {};


export default class NewTextInput extends Component<Props, State> {
    [x: string]: any;
    // state = {site:'site'};
    state = {

    }


    constructor(props: Props | Readonly<Props>) {
        super(props);
        this.InputRef = React.createRef();

    }

    render() {
        const {state} = this;
        // const locale: any = (this.props as any).locale;
        // const [num,setNum] = useState('');//红包个数
        var num = (this.props as any).num;
        var setNum = (this.props as any).setNum;

        return (
            (this.props as any).type=="1"?
            <TextInput
                ref={this.InputRef}
                // underlineColorAndroid={"transparent"}
                placeholder={(this.props as any).locale._("RedEnvelope.NumberTip")}
                value={num}
                keyboardType={"numeric"}
                onChangeText={(value) => {
                    // var replace = (new BigNumber((new BigNumber(value)).toFixed(4, BigNumber.ROUND_DOWN))).toString();
                    // @ts-ignore
                    var replace = value.match(/^\d*(\.?\d{0,0})/g)[0];
                    value = replace.replace(".","");
                    if(parseInt(value)>200)
                    {
                        Toast.show((this.props as any).locale._("RedEnvelope.AlertNum"))
                        setNum("200");
                        return;
                    }
                    setNum(value);
                }}
                onFocus={()=>{
                    this.listener = Keyboard.addListener("keyboardDidHide",()=>{
                        this.InputRef.current.blur();
                        this.listener.remove();
                    })
                }}
                style={{
                    // backgroundColor:"pink",
                    fontSize:width/360*15,
                        height:width/360*25,
                        color:'black',
                        padding:0,
                        fontWeight:"bold",
                        textAlign:"left",
                        paddingLeft:width*0.01,
                        width:width*0.35,
                        borderBottomWidth:1,
                }}
            />
            :
            <TextInput
                ref={this.InputRef}
                underlineColorAndroid={"transparent"}
                placeholder={(this.props as any).locale._("RedEnvelope.Amount")}
                value={num}
                onChangeText={(value) => {
                    // var replace = (new BigNumber((new BigNumber(value)).toFixed(4, BigNumber.ROUND_DOWN))).toString();
                    // @ts-ignore
                    var replace = value.match(/^\d*(\.?\d{0,2})/g)[0];
                    value = replace;
                    setNum(value);
                    if(parseFloat(value)/parseFloat(num)<0.05)
                    {
                        Toast.show((this.props as any).locale._("RedEnvelope.AlertTotal"));
                    }

                }}
                onFocus={()=>{
                    this.listener = Keyboard.addListener("keyboardDidHide",()=>{
                        this.InputRef.current.blur();
                        this.listener.remove();
                    })
                }}
                keyboardType={"numeric"}
                style={{
                    fontSize:width/360*15,
                    height:width/360*25,
                    color:'black',
                    padding:0,
                    fontWeight:"bold",
                    textAlign:"left",
                    paddingLeft:width*0.01,
                    width:width*0.35,
                    borderBottomWidth:1,
                }}
            />

        );
    }
}
